package org.mech.sefer.event;

import java.util.EventObject;

import org.mech.sefer.event.bus.EventBus;

/**
 * <p>
 * The Event is abstract event, that is full event object but extends
 * functionality to dispatching itself to his handlers.
 * </p>
 * <p>
 * Event can be killed or forced what affect behaviour of dispatching in
 * {@link EventBus}
 * </p>
 * <p>
 * Date: 24.4.2012 8:40:13
 * </p>
 * 
 * @param <HANDLER>
 *            the handler type
 * @author martin.krajc
 */
public abstract class Event<HANDLER extends Handler> extends EventObject {

	private static final long serialVersionUID = -4305638023792911957L;

	private boolean dead;
	private boolean forced;

	/**
	 * Instantiates a new event with source
	 * 
	 * @param source
	 *            the source
	 */
	public Event(final Object source) {
		super(source);
		this.dead = false;
		this.forced = false;
	}

	/**
	 * Instantiates a new event. Source of this event is class of event.
	 */
	public Event() {
		this(Event.class);
	}

	/**
	 * Dispatch event to handler implementation.
	 * 
	 * @param handler
	 *            the handler
	 */
	public abstract void dispatch(HANDLER handler);

	/**
	 * Kill the event. If somebody kill event, it will not be distributed to
	 * other handlers
	 */
	public final void kill() {
		dead = true;
	}

	/**
	 * Force event. In event bus it means, that if other events ocurred when
	 * handling this event instance, other events will be queued until this
	 * event is dispatched to all handlers. Otherwise the behaviour is
	 * synchronous.
	 */
	public final void force() {
		forced = true;
	}

	/**
	 * Checks if the event is dead.
	 * 
	 * @return true, if is dead
	 */
	public boolean isDead() {
		return dead;
	}

	/**
	 * Checks if the event is forced.
	 * 
	 * @return true, if is forced
	 */
	public boolean isForced() {
		return forced;
	}

}
